var  fs = require("fs");
var path = require('path');
var express = require('express');
var app = express();
//获取数据
var apiPath =  path.join(__dirname,'./API');
var getPath = path.join(apiPath,"GET");
var postPath = path.join(apiPath,"POST");
// 文件名数组[ 'data.json', 'user.json' ]
var getFiles = fs.readdirSync(getPath);
var PostFiles = fs.readdirSync(postPath);

//GET
getFiles.forEach(function (fileName) {
    getRegister(fileName,fs.readFileSync(getPath+"/"+fileName,"utf-8"));
});

//POST
PostFiles.forEach(function (fileName) {
    postRegister(fileName,fs.readFileSync(postPath+"/"+fileName,"utf-8"));
});

function format(url) {
    return "/"+url.replace(".json","");
}
function getRegister(url,rsp) {

    app.get(format(url), function (req, res) {
        res.send(rsp);
    });
}

function postRegister(url,rsp) {
    app.post(format(url), function (req, res) {
        res.send(rsp);
    });
}

app.get('/', function (req, res) {
    res.send('Hello World!');
});





app.use(express.static('public'));

var server = app.listen(3000, function () {
    var host = server.address().address;
    var port = server.address().port;

    console.log('Example app listening at http://%s:%s', host, port);
});